<!--
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-08-26 17:47:16
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-25 21:54:54
 * @FilePath: /turo-repo-enterprise/apps/fuck-enterprise-admin/src/views/system/permission/components/edit.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-drawer v-model="visible" title="权限编辑">
    <el-form ref="formRef" :model="modelValue" :rules="rules" label-position="top">
      <el-form-item label="权限编码" prop="permissionCode">
        <el-input v-model="modelValue.permissionCode"></el-input>
      </el-form-item>
      <el-form-item label="权限名称" prop="permissionName">
        <el-input v-model="modelValue.permissionName"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button @click="submit" type="primary">确定</el-button>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { fetchPermissionInfo, savePermission, editPermission } from "@/api/modules/permission";
import { ResultEnum } from "@/enums/httpEnum";
import { EmitProps } from "@/typings/interface";
import { PermissionSchema } from "@/schema";
import { isNotEmpty } from "@/utils/isHelper";
import { ElNotification, FormInstance } from "element-plus";
import { nextTick, watch } from "vue";
import { reactive } from "vue";
import { ref } from "vue";

const visible = ref<boolean>(false);

const modelValue = reactive<PermissionSchema>({
  permissionCode: "",
  permissionName: ""
});

const rules = {
  permissionCode: [{ required: true, trigger: "blur", message: "请输入权限编码" }],
  permissionName: [{ required: true, trigger: "blur", message: "请输入权限名称" }]
};

const formRef = ref<FormInstance>();
const emit = defineEmits<EmitProps>();
function submit() {
  formRef.value?.validate().then(valid => {
    if (valid) {
      if (modelValue?.id) {
        edit();
      } else {
        save();
      }
    }
  });
}

function edit() {
  editPermission({ id: modelValue?.id }, modelValue).then(r => {
    const { code, message } = r;
    if (code === ResultEnum.SUCCESS) {
      visible.value = false;
      emit("success");
      ElNotification.success("编辑成功！");
    } else {
      ElNotification.error(message);
    }
  });
}

function save() {
  savePermission(modelValue).then(r => {
    const { code, message } = r;
    if (code === ResultEnum.SUCCESS) {
      visible.value = false;
      emit("success");
      ElNotification.success("添加成功！");
    } else {
      ElNotification.error(message);
    }
  });
}

defineExpose({
  open(row) {
    if (isNotEmpty(row) && isNotEmpty(row?.id)) {
      fetchPermissionInfo({ id: row.id }).then(r => {
        const { code, data, message } = r;
        if (code == ResultEnum.SUCCESS) {
          nextTick(() => {
            Object.assign(modelValue, data);
          });
        } else {
          ElNotification.error(message);
        }
      });
    }
    visible.value = true;
  }
});

watch(
  () => visible.value,
  newVal => {
    if (!newVal) {
      nextTick(() => formRef.value?.resetFields());
      modelValue.id = "";
    }
  }
);
</script>
